/**
 * ReportGear(2011)
 */
package com.reportgear.designer.gui.config;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JMenu;
import javax.swing.JPopupMenu;
import javax.swing.JToolBar;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;

import net.abbp.swing.util.GuiUtils;
import net.abbp.swing.util.ImageUtils;

/**
 * 菜单定义
 * 
 * @version 1.0 2011-4-26
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class MenuDef extends ShortCut implements Cloneable {
	protected String name;
	protected char mnemonic;
	protected String iconPath;
	protected String tooltip;
	private List<ShortCut> shortcutList = new ArrayList<ShortCut>();
	protected boolean enabled = true;
	protected JMenu createdJMenu;
	protected JButton createdButton;
	protected JPopupMenu popupMenu;

	private MenuListener menuDefListener = new MenuListener() {
		public void menuCanceled(MenuEvent evt) {
		}

		public void menuDeselected(MenuEvent evt) {
		}

		public void menuSelected(MenuEvent evt) {
			Object source = evt.getSource();
			if (!(source instanceof JMenu)) {
				return;
			}

			updateMenu();
		}
	};

	private MouseListener mouseListener = new MouseAdapter() {
		public void mouseReleased(MouseEvent evt) {
			Object source = evt.getSource();
			JButton btn = (JButton) source;

			if (!(btn.isEnabled())) {
				return;
			}

			JPopupMenu popupMenu = new JPopupMenu();
			popupMenu.setInvoker(btn);

			updatePopupMenu(popupMenu);
			GuiUtils.showPopupMenu(popupMenu, btn, 0, btn.getSize().height);
		}
	};

	public MenuDef() {
	}

	public MenuDef(String name) {
		setName(name);
	}

	public MenuDef(String name, char paramChar) {
		setName(name);
		setMnemonic(paramChar);
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public char getMnemonic() {
		return this.mnemonic;
	}

	public void setMnemonic(char mnemonic) {
		this.mnemonic = mnemonic;
	}

	public String getIconPath() {
		return this.iconPath;
	}

	public void setIconPath(String iconPath) {
		this.iconPath = iconPath;
	}

	public int getShortCutCount() {
		return this.shortcutList.size();
	}

	public ShortCut getShortCut(int index) {
		return this.shortcutList.get(index);
	}

	public void addShortCut(ShortCut shortCut) {
		this.shortcutList.add(shortCut);
	}

	public void clearShortCuts() {
		this.shortcutList.clear();
	}

	public JButton createJButton() {
		if (this.createdButton == null) {
			if (this.iconPath != null) {
				this.createdButton = new JButton(ImageUtils.readIcon(this.iconPath));
			} else if (this.name != null) {
				this.createdButton = new JButton(this.name);
			} else {
				this.createdButton = new JButton("");
			}

			this.createdButton.setToolTipText(this.tooltip);
			this.createdButton.addMouseListener(this.mouseListener);
		}
		return this.createdButton;
	}

	public void setTooltip(String tooltip) {
		this.tooltip = tooltip;
	}

	public JMenu createJMenu() {
		if (this.createdJMenu == null) {
			this.createdJMenu = new JMenu(getName());
			this.createdJMenu.setMnemonic(getMnemonic());
			if (this.iconPath != null) {
				this.createdJMenu.setIcon(ImageUtils.readIcon(this.iconPath));
			}
			this.createdJMenu.addMenuListener(this.menuDefListener);
		}
		return this.createdJMenu;
	}

	public JPopupMenu createJPopupMenu() {
		JMenu menu = createJMenu();
		updateMenu();
		return menu.getPopupMenu();
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
		if (this.createdButton != null) {
			this.createdButton.setEnabled(this.enabled);
		}

		if (this.createdJMenu == null) {
			return;
		}

		this.createdJMenu.setEnabled(this.enabled);
	}

	public boolean isEnabled() {
		return this.enabled;
	}

	public void updateMenu() {
		updatePopupMenu(createJMenu().getPopupMenu());
		if (this.createdJMenu == null) {
			return;
		}

		this.createdJMenu.setEnabled(this.createdJMenu.getPopupMenu().getComponentCount() > 0);
		this.createdJMenu.repaint(10L);
	}

	protected void updatePopupMenu(JPopupMenu menu) {
		menu.removeAll();
		this.popupMenu = menu;

		for (ShortCut sc : this.shortcutList) {
			if (sc instanceof SeparatorDef) {
				SeparatorDef.DEFAULT.intoJPopupMenu(menu);
			} else {
				sc.intoJPopupMenu(menu);
			}
		}
	}

	public boolean intercept2JPopupMenu() {
		updateMenu();

		return (createJMenu().getItemCount() <= 0);
	}

	public void intoJPopupMenu(JPopupMenu menu) {
		menu.add(createJMenu());
	}

	public boolean intercept2JToolBar() {
		updateMenu();

		return false;
	}

	public void intoJToolBar(JToolBar toolbar, Map<String, ButtonGroup> group) {
		toolbar.add(createJButton());
	}

	public JPopupMenu getPopupMenu() {
		return this.popupMenu;
	}

	public void setPopupMenu(JPopupMenu paramJPopupMenu) {
		this.popupMenu = paramJPopupMenu;
	}
}
